package com.android.adviserapplication.sqlite;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CategoryDataDAO {

	private static final String TABLE_NAME = "quiz_table";
	private static final String COLUMN_ID = "qid";
	private static final String COLUMN_ENGLISH = "english_word";
	private static final String COLUMN_JAPANESE = "japanese_word";
	private static final String COLUMN_LEVEL = "qlevel";
	private static final String[] COLUMNS = { COLUMN_ID, COLUMN_ENGLISH,
			COLUMN_JAPANESE, COLUMN_LEVEL };

	private SQLiteDatabase db;

	public CategoryDataDAO(SQLiteDatabase db) {
		this.db = db;
	}

	public long insert(CategoryData qd) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_ENGLISH, qd.getEnglish());
		values.put(COLUMN_JAPANESE, qd.getJapanese());
		values.put(COLUMN_LEVEL, qd.getLevel());
		return db.insert(TABLE_NAME, null, values);
	}

	public int update(CategoryData qd) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_ENGLISH, qd.getEnglish());
		values.put(COLUMN_JAPANESE, qd.getJapanese());
		values.put(COLUMN_LEVEL, qd.getLevel());

		String whereClause = "qid = " + qd.getID();

		return db.update(TABLE_NAME, values, whereClause, null);
	}

	public List<CategoryData> findAll() {
		List<CategoryData> qdList = new ArrayList<CategoryData>();
		Cursor cursor = db.query(TABLE_NAME, COLUMNS, null, null, null, null,
				COLUMN_ID); // ID�Ń\�[�g���ă��X�g��Ԃ�
		while (cursor.moveToNext()) {
			CategoryData qd = new CategoryData();
			qd.setID(cursor.getInt(0));
			qd.setEnglish(cursor.getString(1));
			qd.setJapanese(cursor.getString(2));
			qd.setLevel(cursor.getInt(3));
			qdList.add(qd);
		}
		return qdList;
	}

	public CategoryData findById(int id) {
		String selection = "qid = " + id;
		List<CategoryData> qdList = new ArrayList<CategoryData>();
		Cursor cursor = db.query(TABLE_NAME, COLUMNS, selection, null, null,
				null, null); // ID�Ń\�[�g���ă��X�g��Ԃ�
		while (cursor.moveToNext()) {
			CategoryData qd = new CategoryData();
			qd.setID(cursor.getInt(0));
			qd.setEnglish(cursor.getString(1));
			qd.setJapanese(cursor.getString(2));
			qd.setLevel(cursor.getInt(3));
			return qd;
		}
		return null;
	}

	public int delete(int qid) {
		String whereClause = "qid = " + qid;
		return db.delete(TABLE_NAME, whereClause, null);
	}
}
